package com.HuangJiaQing.www.controller;

import com.HuangJiaQing.www.model.Bill;
import com.HuangJiaQing.www.model.Orders;
import com.HuangJiaQing.www.service.BillService;
import com.HuangJiaQing.www.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * @author HONOR
 * @date 2022/8/6 11:25
 */
@Controller
public class BillController {
    @Autowired
    private BillService billService;
    @Autowired
    private OrdersService ordersService;

    @RequestMapping(value = "/toAddBill",method = RequestMethod.GET )
    public String toAddBill(Model model){
        List<Orders> orders = ordersService.selectOrdersNeedCourier();
        model.addAttribute("orders",orders);
        return "addBill";
    }

    @RequestMapping(value = "/addBillToAddBill/{id}",method = RequestMethod.POST)
    public String addBillToAddBill(@PathVariable("id")Integer id){
        Orders orders = ordersService.selectOrdersById(id);
        billService.insertBill(orders);
        return "redirect:/toAddBill";
    }

    @RequestMapping(value = "/addBillToCollectBill/{id}",method = RequestMethod.POST)
    public String addBillToCollectBill(@PathVariable("id")Integer id){
        Orders orders = ordersService.selectOrdersById(id);
        billService.insertBill(orders);
        return "redirect:/toCollectBill";
    }

    @RequestMapping(value = "/updateBill/{id}",method = RequestMethod.PUT)
    public String completeBill(@PathVariable("id")Integer id){
        billService.completeBillByCourier(id);
        return "redirect:/courierLogin";
    }


    @RequestMapping(value = "/toAgreeBill",method = RequestMethod.GET)
    public String toAgreeBill(Model model){
        List<Bill> bills = billService.selectBillDisagree();
        model.addAttribute("bills",bills);
        return "agreeBill";
    }

    @RequestMapping(value = "/agreeBill/{id}",method = RequestMethod.PUT)
    public String agreeBill(@PathVariable("id")Integer id){
        billService.completeBillByUser(id);
        return "redirect:/toAgreeBill";
    }

    @RequestMapping(value = "/toCollectBill",method = RequestMethod.GET)
    public String toCollectBill(Model model){
        List<Orders> orders = ordersService.selectOrdersCollected();
        model.addAttribute("orders",orders);
        return "collectBill";
    }
}
